package com.hyj.shop.project.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hyj.shop.project.utils.controller.UploadFileController;
import com.hyj.shop.project.utils.domain.AjaxResult;
import com.hyj.shop.project.user.domain.UserInfo;
import com.hyj.shop.project.user.mapper.UserInfoMapper;
import com.hyj.shop.project.user.service.UserInfoService;
import com.hyj.shop.project.utils.service.Impl.UploadFileImpl;
import com.hyj.shop.project.utils.service.UploadFileService;
import com.hyj.shop.utils.StringUtil;
import jdk.nashorn.internal.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.FileSystemException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UploadFileService uploadFileService;
    @Override
    public AjaxResult updateUserInfo(MultipartFile file, String content, String type){
        System.out.println("服务层");
        System.out.println(type.equals("avtar"));
        Map map = JSONObject.parseObject(content);
        System.out.println(map);
        System.out.println(type);
        try{
            if(type.equals("avatar")){ // 修改头像
                System.out.println("拿到前端的文件");
                // 获取文件的名称
                String fileName = file.getOriginalFilename();

                // 使用工具类根据上传文件生成唯一图片名称
                String imgName = StringUtil.getRandomImgName(fileName);
                if (!file.isEmpty()) {
                    FileInputStream inputStream = (FileInputStream) file.getInputStream();
                    String path = uploadFileService.uploadQNImg(inputStream, imgName);
                    System.out.print("七牛云返回的图片链接:" + path);
                    map.put("avatar",path);
                    int flag =  userInfoMapper.updateUserInfo(map,type);
                    AjaxResult ajaxResult= AjaxResult.success();
                    ajaxResult.put("url",path);
                    return ajaxResult;
                }
                AjaxResult ajaxResult= AjaxResult.error();
                return ajaxResult;

            } else { // 修改文本内容
                System.out.println("修改文本内容");
                int flag = userInfoMapper.updateUserInfo(map,type);
                userInfoMapper.updateArticleComment(map,type);
                System.out.println(flag);
                AjaxResult ajaxResult= AjaxResult.success();
                return ajaxResult;
            }
        } catch (Exception e){
            System.out.println("出现错误");
            System.out.println(e);
            AjaxResult ajaxResult= AjaxResult.error(e.toString());
            return ajaxResult;

        }
    }

    @Override
    public AjaxResult updateNickName(UserInfo userInfo) {
        System.out.println(userInfo+"-------------");


        AjaxResult ajaxResult =AjaxResult.success("更新成功");
        ajaxResult.put("code",200);

        return ajaxResult;

    }

    @Override
    public AjaxResult updateSexy(UserInfo userInfo) {
        userInfoMapper.updateSexy(userInfo);
        AjaxResult ajaxResult =AjaxResult.success("更新成功");
        ajaxResult.put("code",200);

        return ajaxResult;
    }

    @Override
    public AjaxResult updateBirthday(UserInfo userInfo) {
        userInfoMapper.updateBirthday(userInfo);
        AjaxResult ajaxResult =AjaxResult.success("更新成功");
        ajaxResult.put("code",200);

        return ajaxResult;

    }

    @Override
    public AjaxResult updateAvatar(int userId, String path) {
        userInfoMapper.updateAvatar(userId,path);
        return null;
    }

    @Override
    public AjaxResult queryUserInfo(int userId) {
        UserInfo userInfo=userInfoMapper.queryUserInfo(userId);
        AjaxResult ajaxResult =AjaxResult.success("查询成功");
        ajaxResult.put("code",200);
        ajaxResult.put("data",userInfo);
        return ajaxResult;
    }
}
